/**
 * @author Renan Moura Batista 
 * Esta classe e responsavel por invocar os metodos principais CRUD  no BD , deve
 * ser herdada por todas os models que utilizarem consultas ao banco
 */
package models;

import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;


public class DbRequestsModel extends FactoryConnection {
    
    /**
     * 
     * @param table
     * @param fields
     * @param values
     * @return 
     */
    protected boolean insert ( String table , String fields[], String values[] ) 
    {
        
      return true;
        
    }
    
    /**
     * 
     * @param where
     * @param fields 
     * @param values 
     * @param table 
     * @return 
     */
    protected boolean update ( String table,String fields[], String values[] , String where ) 
    {
        
        return true;
        
    }
    /**
     * 
     * @param table
     * @param where
     * @return 
     */
    protected boolean delete (String table, String where ) 
    {
        
         try {
             
           return this.action.execute("DELETE FROM "+table+ "WHERE" +where+";");
           
        } catch ( SQLException e ) {
            
           JOptionPane.showMessageDialog(null, "Erro Interno na conexão com o banco: 'Código D001'");
             
           return false;
        }
         
    }
    
    /**
     * 
     * @param tables
     * @param fields
     * @param where
     * @return 
     */
    protected Object select(String tables, String fields, String where) {
        String frase = null;
       // List<String> values= new ArrayList<>();
        
        List<Object> response = new ArrayList<>();
        
        DtoModel dataDB;
        
        String[] arrayFields = fields.split(",");
        
        int fieldsNumber = arrayFields.length;
        
        String[] values = new String[fieldsNumber+1];
        
        int ite = 1;
        
        try {

            ResultSet result = this.action.executeQuery(" SELECT " + fields + " FROM " + tables + " WHERE " + where);
            while (result.next()) {
                ite = 1;
                while(ite <= fieldsNumber ){
                       values[ite] =  result.getString(ite);
                       ite++;
                }
                
                dataDB = new DtoModel();
                dataDB.setValues( values );
                
                response.add( dataDB );
                /**System.out.println(values[1]+","+values[2]);*/
                /*response.add(values);*/
                
                frase += "nome : " + result.getString(1) + "| numero:" + result.getString(2) + "\n";

            }
            System.out.println(frase);

            return result;

        } catch (SQLException e) {

            JOptionPane.showMessageDialog(null, "Erro Interno na conexão com o banco: 'Código S001'");

            return null;
        }

    }
}
